//
//  OMSalesRankViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/4/25.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMSalesRankViewController.h"
#import "OMConsumerGoodsCollectDelegater.h"
#import "OMGoodOneColumnLayout.h"
#import "OMProduct.h"
@interface OMSalesRankViewController ()
/** 热销商品数组*/
@property (nonatomic, strong) NSArray *goods;
/** 展示UI*/
@property (nonatomic, strong) UICollectionView *collectView;
/** collectView代理,给controller瘦身*/
@property (nonatomic, strong) OMConsumerGoodsCollectDelegater *delegater;
@end

@implementation OMSalesRankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self getSalesRank];
}


/**
 初始化UI
 */
- (void)setupUI
{
    self.automaticallyAdjustsScrollViewInsets = false;
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[OMGoodOneColumnLayout new]];
    _collectView.backgroundColor = SM_whiteColor;
    _delegater = [[OMConsumerGoodsCollectDelegater alloc]initWithCollectionView:_collectView];
    [self.view addSubview:_collectView];
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.top.equalTo(self.navigationBar.mas_bottom);
    }];
}


/**
 网络请求重载
 */
- (void)reloadDataFromNetWork
{
    [self getSalesRank];
}


/**
 获取热销排行商品s
 */
- (void)getSalesRank
{
    [self startAnimating:true];
    [KTHttpUtils GET:@"index/hotGoods.jhtml" completion:^(id responseObject, BOOL isSuccess, NSString *message) {
        if (isSuccess) {
            [self startAnimating:false];
            NSArray<OMProduct*>*products = [OMProduct mj_objectArrayWithKeyValuesArray:responseObject[OMDataKey]];
            for (OMProduct *product in products) {
                product.image = [responseObject[OMImagePrefix] stringByAppendingString:product.image?:@""];
            }
            self.goods = products;
        }else {
            [self showNetWorkError];
            [OMHUD HUDInfo:message];
        }
    }];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    SMNotificationPost(SM_NOTIFACATION_NEED_CARTITEM_SHOW, nil);
}


- (void)setGoods:(NSArray *)goods
{
    _goods = goods;
    self.delegater.goods = goods;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
